﻿#include "SurfaceElectricFieldMapSettingDialog.h"
#include "ScalarBarSetting.h"
#include <QDebug>
#include <QTableWidget>
#include <QHeaderView>
#include <QSlider>
#include <QLabel>
#include <QLineEdit>
#include <BaseWidget/DoubleSpinBox.h>
#include "SurfaceElectricFieldMapSettingData.h"
#include "IdaWidgetHelper.h"
#include "IdaDialogMsg.h"
#include "BaseModule/GlobalHelper.h"
#include "BaseModule/IBaseSignal.h"

namespace pst
{
    SurfaceElectricFieldMapSettingDialog::SurfaceElectricFieldMapSettingDialog(QWidget* parent)
        : IdaDialogFrame(parent)
        , m_geometryRowCount(0)
        , m_resultDataRowCount(0)
        , m_currentData(nullptr)
        , m_cubeAxesRowCount(0)
    {
        ui.setupUi(this);
        //setWindowButtonType(0);
        setNoCloseWindow(true);
        setWindowTitle("表面分布图");
        m_scalarBarSetting = new ScalarBarSetting(this);
        ui.scalarBarLayout->addWidget(m_scalarBarSetting);
        ui.pushButton_apply->setDefault(true);
        addGeometrySettingToUI();
        addResultDataSettingToUI();
        addCubeAxesActorSettingToUI();
        IdaWidgetHelperInstance->setMainButtonStyle(ui.pushButton_confirm); // 将确定设置为主按钮
        m_geoTransparencySlider->setFixedHeight(14);
        m_resultDataTransparencySlider->setFixedHeight(14);
        initConnections(); // initConnections放在各个组件初始化之后;
    }

    SurfaceElectricFieldMapSettingDialog::~SurfaceElectricFieldMapSettingDialog()
    {
    }

    void SurfaceElectricFieldMapSettingDialog::setMainWindowId(int id)
    {
        m_mainWindowId = id;
    }

    int SurfaceElectricFieldMapSettingDialog::getMainWindowId() const
    {
        return m_mainWindowId;
    }

    void SurfaceElectricFieldMapSettingDialog::setType(EntryType type)
    {
        switch (type)
        {
        case pst::SurfaceElectricFieldMapSettingDialog::EntryType::ScalarBar:
            ui.tabWidget->setCurrentIndex(0);
            break;
        case pst::SurfaceElectricFieldMapSettingDialog::EntryType::Geometry:
            ui.tabWidget->setCurrentIndex(1);
            break;
        case pst::SurfaceElectricFieldMapSettingDialog::EntryType::ResultData:
            ui.tabWidget->setCurrentIndex(2);
            break;
        default:
            break;
        }
    }

    void SurfaceElectricFieldMapSettingDialog::setScalarBarSettingUIEnabled(bool enable)
    {
        ui.tabWidget->setTabEnabled(0, enable);
    }

    bool SurfaceElectricFieldMapSettingDialog::getScalarBarSettingUIEnabled() const
    {
        return ui.tabWidget->isTabEnabled(0);
    }

    bool SurfaceElectricFieldMapSettingDialog::updateUI(SurfaceElectricFieldMapSettingData* data, EntryType type)
    {
        if (data == nullptr)
        {
            return false;
        }

        ui.tabWidget->setCurrentIndex(int(type));

        auto _scalarBarSettingData = data->getScalarBarSettingData();
        if (_scalarBarSettingData == nullptr)
        {
            ui.tabWidget->setTabEnabled(0, false);
        }
        else
        {
            ui.tabWidget->setTabEnabled(0, true);
            m_scalarBarSetting->setUseScalarBarAutoRange(_scalarBarSettingData->getScalarBarAutoRange());
            m_scalarBarSetting->setMaxValue(_scalarBarSettingData->getScalarBarMaxValue());
            m_scalarBarSetting->setMinValue(_scalarBarSettingData->getScalarBarMinValue());
            m_scalarBarSetting->setIsShowTitle(_scalarBarSettingData->getScalarBarIsShowTitle());
            m_scalarBarSetting->setTitle(_scalarBarSettingData->getScalarBarTitle());
            m_scalarBarSetting->setTitleColor(_scalarBarSettingData->getScalarBarTitleColor());
            m_scalarBarSetting->setFontSize(_scalarBarSettingData->getScalarBarFontSize());
        }

        auto _geometrySettingData = data->getGeometrySettingData();
        if (_geometrySettingData == nullptr)
        {
            ui.tabWidget->setTabEnabled(1, false);
        }
        else
        {
            ui.tabWidget->setTabEnabled(1, true);
            m_geoTransparencySlider->setValue(_geometrySettingData->getGeometryTransparency());
        }

        auto _resultDataSettingData = data->getResultDataSettingData();
        if (_resultDataSettingData == nullptr)
        {
            ui.tabWidget->setTabEnabled(2, false);
        }
        else
        {
            ui.tabWidget->setTabEnabled(2, true);
            m_resultNameTextLineEdit->setText(_resultDataSettingData->getResultDataName());
            m_resultDataTransparencySlider->setValue(_resultDataSettingData->getResultDataTransparency());
            //resultScaleFactory->setValue(_resultDataSettingData->getResultDataScaleFactor());
        }
        auto _cubeAxesActorSettingData = data->getCubeAxesActorSettingData();
        if (_cubeAxesActorSettingData == nullptr)
        {
            ui.tabWidget->setTabEnabled(3, false);
        }
        else
        {
            ui.tabWidget->setTabEnabled(3, true);
            m_isCubeAxesActorVisible->setChecked(_cubeAxesActorSettingData->getVisibility());
        }

        m_currentData = data;

        return true;
    }

    void SurfaceElectricFieldMapSettingDialog::initConnections()
    {
        connect(m_geoTransparencySlider, &QSlider::valueChanged, m_geoTransparencyLabel,
            [this](int value)
            { m_geoTransparencyLabel->setText(QString::number(value) + "%"); });

        connect(m_resultDataTransparencySlider, &QSlider::valueChanged, m_resultDataTransparencyLabel,
            [this](int value)
            { m_resultDataTransparencyLabel->setText(QString::number(value) + "%"); });
    }

    void SurfaceElectricFieldMapSettingDialog::setScalarBarCustomRange(double min, double max)
    {
        if (m_currentData == nullptr)
        {
            return;
        }
        m_scalarBarSetting->setMinValue(min);
        m_scalarBarSetting->setMaxValue(max);
    }

    void SurfaceElectricFieldMapSettingDialog::addGeometrySettingToUI()
    {
        QTableWidget* tableWidget = new QTableWidget(this);
        tableWidget->setRowCount(1);
        tableWidget->setColumnCount(2);
        tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "值");
        tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        // 透明度滑动条
        QWidget* sliderWidget = new QWidget(this);
        QHBoxLayout* sliderLayout = new QHBoxLayout(sliderWidget);
        // BaseWidget::DoubleSpinBox* transparencySpinBox = new BaseWidget::DoubleSpinBox(this);
        m_geoTransparencySlider = new QSlider(Qt::Horizontal, this);
        m_geoTransparencyLabel = new QLabel("0%", this);
        m_geoTransparencySlider->setRange(0, 100);
        sliderLayout->addWidget(m_geoTransparencySlider);
        sliderLayout->addWidget(m_geoTransparencyLabel);
        // sliderLayout->addWidget(transparencySpinBox);
        sliderLayout->setStretchFactor(m_geoTransparencySlider, 5);
        sliderLayout->setStretchFactor(m_geoTransparencyLabel, 1);
        tableWidget->setItem(m_geometryRowCount, 0, new QTableWidgetItem("几何图像透明度"));
        tableWidget->setCellWidget(m_geometryRowCount, 1, sliderWidget);
        IdaWidgetHelperInstance->setTableWidgetStyle(tableWidget);
        ui.gridLayout_geomtry->addWidget(tableWidget);
    }

    void SurfaceElectricFieldMapSettingDialog::addResultDataSettingToUI()
    {
        QTableWidget* tableWidget = new QTableWidget(this);
        tableWidget->setRowCount(2);
        tableWidget->setColumnCount(2);
        tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "值");
        tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        // 数据结果名称
        m_resultNameTextLineEdit = new QLineEdit(this);
        tableWidget->setItem(m_resultDataRowCount, 0, new QTableWidgetItem("数据结果名称"));
        //m_resultNameTextLineEdit->setEnabled(false);
        tableWidget->setCellWidget(m_resultDataRowCount, 1, m_resultNameTextLineEdit);

        // 透明度滑动条
        QWidget* sliderWidget = new QWidget(this);
        QHBoxLayout* sliderLayout = new QHBoxLayout(sliderWidget);
        // BaseWidget::DoubleSpinBox* transparencySpinBox = new BaseWidget::DoubleSpinBox(this);
        m_resultDataTransparencySlider = new QSlider(Qt::Horizontal, this);
        m_resultDataTransparencyLabel = new QLabel("0%", this);
        m_resultDataTransparencySlider->setRange(0, 100);
        sliderLayout->addWidget(m_resultDataTransparencySlider);
        sliderLayout->addWidget(m_resultDataTransparencyLabel);
        // sliderLayout->addWidget(transparencySpinBox);
        sliderLayout->setStretchFactor(m_resultDataTransparencySlider, 5);
        sliderLayout->setStretchFactor(m_resultDataTransparencyLabel, 1);
        tableWidget->setItem(++m_resultDataRowCount, 0, new QTableWidgetItem("结果图像透明度"));
        tableWidget->setCellWidget(m_resultDataRowCount, 1, sliderWidget);

        //resultScaleFactory = new BaseWidget::DoubleSpinBox(this);
        //resultScaleFactory->setRange(0.000001, 1000000000);
        //resultScaleFactory->setValue(1.0);
        //tableWidget->setItem(++m_resultDataRowCount, 0, new QTableWidgetItem("结果图比例"));
        //tableWidget->setCellWidget(m_resultDataRowCount, 1, resultScaleFactory);
        IdaWidgetHelperInstance->setTableWidgetStyle(tableWidget);
        ui.gridLayout_resultData->addWidget(tableWidget);
    }

    void SurfaceElectricFieldMapSettingDialog::addCubeAxesActorSettingToUI()
    {
        QTableWidget* tableWidget = new QTableWidget(this);
        tableWidget->setRowCount(1);
        tableWidget->setColumnCount(2);
        tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "值");
        tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        tableWidget->setItem(m_cubeAxesRowCount, 0, new QTableWidgetItem("可见性"));
        m_isCubeAxesActorVisible = new QCheckBox(this);
        tableWidget->setCellWidget(m_cubeAxesRowCount, 1, m_isCubeAxesActorVisible);

        ui.gridLayout_cubeAxes->addWidget(tableWidget);
    }

    void SurfaceElectricFieldMapSettingDialog::updateUsingDataFromUI()
    {
        if (m_currentData == nullptr)
        {
            return;
        }

        auto _scalarBarSettingData = m_currentData->getScalarBarSettingData();
        if (_scalarBarSettingData != nullptr)
        {
            _scalarBarSettingData->setScalarBarAutoRange(m_scalarBarSetting->getUseScalarBarAutoRange());
            _scalarBarSettingData->setScalarBarMaxValue(m_scalarBarSetting->getMaxValue());
            _scalarBarSettingData->setScalarBarMinValue(m_scalarBarSetting->getMinValue());
            _scalarBarSettingData->setScalarBarIsShowTitle(m_scalarBarSetting->getIsShowTitle());
            _scalarBarSettingData->setScalarBarTitle(m_scalarBarSetting->getTitle());
            _scalarBarSettingData->setScalarBarTitleColor(m_scalarBarSetting->getTitleColor());
            _scalarBarSettingData->setScalarBarFontSize(m_scalarBarSetting->getFontSize());
        }

        auto _geometrySettingData = m_currentData->getGeometrySettingData();
        if (_geometrySettingData != nullptr)
        {
            _geometrySettingData->setGeometryTransparency(m_geoTransparencySlider->value());
        }

        auto _resultDataSettingData = m_currentData->getResultDataSettingData();
        if (_resultDataSettingData != nullptr)
        {
            //emit auto isValid = signal_checkResultNameIsValid(m_currentData, m_resultNameTextLineEdit->text());
            //if (isValid)
            //{
            _resultDataSettingData->setResultDataName(m_resultNameTextLineEdit->text());
            //}
            _resultDataSettingData->setResultDataTransparency(m_resultDataTransparencySlider->value());
            //_resultDataSettingData->setResultDataScaleFactor(resultScaleFactory->value());
        }

        auto _cubeAxesSettingData = m_currentData->getCubeAxesActorSettingData();
        if (_cubeAxesSettingData != nullptr)
        {
            _cubeAxesSettingData->setVisibility(m_isCubeAxesActorVisible->isChecked());
        }

        auto dataID = m_currentData->getID();
        bool scalarBarIsAutoRange = m_scalarBarSetting->getUseScalarBarAutoRange();
        double scalarBarMin = m_scalarBarSetting->getMinValue();
        double scalarBarMax = m_scalarBarSetting->getMaxValue();
        bool scalarBarIsShowTitle = m_scalarBarSetting->getIsShowTitle();
        QString scalarBarTitle = m_scalarBarSetting->getTitle();
        QColor scalarBarFontColor = m_scalarBarSetting->getTitleColor();
        QList<int> scalarBarFontColorList = { scalarBarFontColor.red(), scalarBarFontColor.green(), scalarBarFontColor.blue() };
        QString scalarBarFontColorStr = GlobalHelperInstance->convertIntListToQString(scalarBarFontColorList);
        int scalarBarFontSize = m_scalarBarSetting->getFontSize();

        int geometryTransparency = m_geoTransparencySlider->value();

        QString resultGraphName = m_resultNameTextLineEdit->text();
        int resultGraphModelTransparency = m_resultDataTransparencySlider->value();

        bool cubeAxesIsVisible = m_isCubeAxesActorVisible->isChecked();

        QStringList _pyCodes{};
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1 = PostProcessing.SurfaceElectricFieldMapMainSetting()");
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setWindowId(%1)").arg((int)m_mainWindowId);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setType(%1)").arg((int)0);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setSettingDataId(%1)").arg((int)dataID);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setScalarBarData(%1,%2,%3,%4,\"%5\",\"%6\",%7)").arg(scalarBarIsAutoRange).arg(scalarBarMin).arg(scalarBarMax).arg(scalarBarIsShowTitle).arg(scalarBarTitle).arg(scalarBarFontColorStr).arg(scalarBarFontSize);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setGeometryData(%1)").arg((int)geometryTransparency);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.setResultGraphData(\"%1\",%2,%3)").arg(resultGraphName).arg((int)resultGraphModelTransparency).arg(cubeAxesIsVisible);
        _pyCodes += QString("surfaceelectricfieldmapmainsetting1.saveSurfaceElectricFieldMapMainSettingData()");

        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
    }

    void SurfaceElectricFieldMapSettingDialog::on_pushButton_cancel_clicked()
    {
        // m_scalarBarSetting->restoreUIFromUsingData();
        // restoreUsingGeometryActorDataToUI();
        // restoreUsingResultActorDataToUI();
        // updateUsingDataFromUI();
        this->hideWindow();
        m_currentData == nullptr;
    }

    void SurfaceElectricFieldMapSettingDialog::on_pushButton_apply_clicked()
    {
        if(!m_scalarBarSetting->getUseScalarBarAutoRange())
        {
            auto _maxValue = m_scalarBarSetting->getMaxValue();
            auto _minValue = m_scalarBarSetting->getMinValue();
            if (_maxValue == _minValue)
            {
                IdaDialogMsg::showDialog(this, QString("最大值与最小值不得相等，请重新输入"), EDialogMsg::Error, tr("颜色条最值错误"));
                return;
            }
            if (_maxValue < _minValue)
            {
                m_scalarBarSetting->setMaxValue(_minValue);
                m_scalarBarSetting->setMinValue(_maxValue);
            }
        }
        // m_scalarBarSetting->updateUsingDataFromUI();
        // emitScalarBarSettingSignals();

        // updateUsingGeometryActorDataFromUI();
        // emitGeometrySettingSignals();

        // updateUsingResultActorDataFromUI();
        // emitResultDataSettingSignals();
        if (m_currentData->getResultDataSettingData()) //有结果图才需要检测
        {
            emit auto isValid = signal_checkResultNameIsValid(m_currentData, m_resultNameTextLineEdit->text());
            if (!isValid)
            {
                return;
            }
        }
        updateUsingDataFromUI();
        emit signal_updateSetting(m_currentData);
        m_currentData == nullptr;
    }

    void SurfaceElectricFieldMapSettingDialog::on_pushButton_confirm_clicked()
    {
        if (!m_scalarBarSetting->getUseScalarBarAutoRange())
        {
            auto _maxValue = m_scalarBarSetting->getMaxValue();
            auto _minValue = m_scalarBarSetting->getMinValue();
            if (_maxValue == _minValue)
            {
                IdaDialogMsg::showDialog(this, QString("最大值与最小值不得相等，请重新输入"), EDialogMsg::Error, tr("颜色条最值错误"));
                return;
            }
            if (_maxValue < _minValue)
            {
                m_scalarBarSetting->setMaxValue(_minValue);
                m_scalarBarSetting->setMinValue(_maxValue);
            }
        }
        // m_scalarBarSetting->updateUsingDataFromUI();
        // emitScalarBarSettingSignals();

        // updateUsingGeometryActorDataFromUI();
        // emitGeometrySettingSignals();

        // updateUsingResultActorDataFromUI();
        // emitResultDataSettingSignals();
        if (m_currentData->getResultDataSettingData()) //有结果图才需要检测
        {
            emit auto isValid = signal_checkResultNameIsValid(m_currentData, m_resultNameTextLineEdit->text());
            if (!isValid)
            {
                return;
            }
        }
        updateUsingDataFromUI();
        emit signal_updateSetting(m_currentData);
        this->hideWindow();
        m_currentData == nullptr;
    }
} // namespace pst
